﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd.Services;
using enqueteJwVd.DAL;
using enqueteJwVd.ViewModels;
using enqueteJwVd.Helpers;
using System.Web.UI.WebControls;
using System.Collections;
using AutoMapper;


namespace enqueteJwVd.Controllers
{
    [Authorize(Roles = "e-manager")]
    public class SurveysController : Controller
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        ISurveyService surveyService;
        //private VDLDbEntities db = new VDLDbEntities();

        public SurveysController(ISurveyService serveyService)
        {
            this.surveyService = serveyService;
        }

        public SurveysController()
        {

        }

        //
        // GET: /Surveys/

        public ViewResult Index()
        {
            var surveys = surveyService.GetAllSurveys();
            var viewModel = new SurveyIndexViewModel() { Surveys = surveys };
            return View(viewModel);
        }


        //
        // GET: /Surveys/Details/5

        public ViewResult Details(int? id)
        {
            Survey survey = unitOfWork.SurveyRepository.GetById(id);
            return View(survey);
        }

        //
        // GET: /Surveys/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Surveys/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")]Survey survey)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.SurveyRepository.Add(survey);
                unitOfWork.SaveChanges();
                return RedirectToAction("Link", "UserSurveys");
                //return RedirectToAction("Index");
            }

            return View(survey);
        }

        private MultiSelectList GetUsers(string[] selectedValues)
        {
            Hashtable voorAchterNaam = new Hashtable();
            IEnumerable<User> users = unitOfWork.UserRepository.GetAll();
            foreach (User user in users)
            {
                voorAchterNaam.Add(user.Id, (user.Voornaam + " " + user.Achternaam).ToString());

            }
            return new MultiSelectList(voorAchterNaam, voorAchterNaam.Keys.ToString(), voorAchterNaam.Values.ToString(), selectedValues);

        }

        // GET: /Surveys/Activate Survey - link in de indexpage
        public ActionResult ActivateSurvey(int? id)
        {
            Survey survey = unitOfWork.SurveyRepository.GetById(id);
            /*var activateSurveyViewModel = new ActivateSurveyViewModel();
            activateSurveyViewModel.Description = survey.Description;
            activateSurveyViewModel.EndText = survey.EndText;
            activateSurveyViewModel.Id = survey.Id;
            activateSurveyViewModel.Title = survey.Title;
            return View(activateSurveyViewModel);
            
        //ViewBag.UsersList = GetUsers(null);
             * */
            return View(survey);
        }

        /*
        // GET: /Surveys/Activate
        public ActionResult Activate(int? id)
        {   //http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/
           
            Survey survey = unitOfWork.SurveyRepository.GetById(id);
            var activateSurveyViewModel = new ActivateSurveyViewModel();
            activateSurveyViewModel.Description = survey.Description;
            activateSurveyViewModel.EndText = survey.EndText;
            activateSurveyViewModel.Id = survey.Id;
            activateSurveyViewModel.Title = survey.Title;
            return View(activateSurveyViewModel);
        }
         * */
         

        // POST: /Surveys/Activate
        [HttpPost]
        public ActionResult ActivateSurvey(Survey activateSurvey)
        {
            //Survey newSurvey = new Survey();

            if (ModelState.IsValid)
            {
                // Mapper.CreateMap<ActivateSurveyViewModel, Survey>()
                /*.ForMember(dest => dest.Description, opt => opt.Ignore())
                .ForMember(dest => dest.EndText, opt => opt.Ignore())
                .ForMember(dest => dest.Title, opt => opt.Ignore())
                .ForMember(dest => dest.QuestionGroup, opt => opt.Ignore())
                .ForMember(dest => dest.UserSurvey, opt => opt.Ignore())
                .ForMember(dest => dest.Answer, opt => opt.Ignore())
                .ForMember(dest => dest.Anonymous, opt => opt.Ignore());
                 //.ForMember(dest => dest.Id, opt => opt.Ignore())* */
                
                //Mapper.AssertConfigurationIsValid();
                //AutoMapper.Mapper.Map(activateSurvey, newSurvey);
                //List <Survey> surveys = unitOfWork.SurveyRepository.GetAll();

                //unitOfWork.SurveyRepository.Get(from s unitOfWork.sur
                activateSurvey.State = true;
                activateSurvey.ActivationDate = DateTime.Now;
                unitOfWork.SurveyRepository.Update(activateSurvey);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(activateSurvey);
        }


        //
        // GET: /Surveys/Edit/5

        public ActionResult Edit(int? id)
        {
            Survey survey = unitOfWork.SurveyRepository.GetById(id);
            return View(survey);
        }

        //
        // POST: /Surveys/Edit/5

        [HttpPost]
        public ActionResult Edit(Survey survey)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.SurveyRepository.Update(survey);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(survey);
        }

        //
        // GET: /Surveys/Delete/5

        public ActionResult Delete(int? id)
        {
            Survey survey = unitOfWork.SurveyRepository.GetById(id);
            return View(survey);
        }

        //
        // POST: /Surveys/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int? id)
        {
            Survey survey = unitOfWork.SurveyRepository.GetById(id);
            unitOfWork.SurveyRepository.Delete(survey);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }

        [HttpPost]
        public ActionResult Submit(Survey survey)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.SurveyRepository.Add(survey);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(survey);
        }

        [HttpPost]
        public ActionResult SubmitActivate(Survey survey)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.SurveyRepository.Update(survey);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
                //hier moet de send email gebeuren.
            }

            /*
            foreach (var item in this.GetUsersList(survey))
            {
             * //http://stackoverflow.com/questions/8478676/need-to-create-link-to-mvc-page-from-email
                string url = Url.Action("action", "controller",//TO ADAPT
    new System.Web.Routing.RouteValueDictionary(new { id = id }),
    "http", Request.Url.Host);

                MailHelper.SendMailMessage("my.addres@somemail.com",
                    "to.someone@mail.com, someone.other@mail.com",
                    "",
                    "",
                    "Survey" + survey.Title,
                    //"Hier zou de link naar de survey in moeten komen iets als /");
                    url);


                // send email
            }
             * */
            return View(survey);
        }

        private List<User> GetUsersList(Survey survey)
        {
            List<User> usersList = new List<User>();

            IQueryable<User> users = from u in unitOfWork.UserRepository.GetAll()
                                     join s in unitOfWork.SurveyRepository.GetAll()
                                         on u.UserSurvey equals s.UserSurvey
                                     select u;
            foreach (User user in users)
            {
                usersList.Add(user);
            }
            return usersList;
        }
    }
}